# Copyright (c) 2024-2024 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#     http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
cmake_minimum_required(VERSION 3.14.1)
project(IOT_CONNECT)
set(IOTC_TARGET IOTC)
set(IOTC_LIB_NAME "iotc")

# Default compilation target platform OS, default is linux.
option(TARGET_PLATFORM "Target platform" "linux")

# Isolation of compiling module such as WiFi, BLE, and SLE is enable by default.
option(ENABLE_WIFI "Enable WIFI Module support" ON)
option(ENABLE_BLE "Enable BLE Module support" ON)
option(ENABLE_SLE "Enable SLE Module support" ON)

# BLE/SLE role, default is server.
option(BLE_ROLE_SERVER "BLE role, server or client" ON)
option(SLE_ROLE_SERVER "SLE role, server or client" ON)

# OpenSource dependencies such as mbedtls and cJSON are left empty by default.
option(IOTC_INC_MBEDTLS "Mbedtls file include" "")
option(IOTC_INC_CJSON "CJson file include" "")
option(IOTC_INK_MBEDTLS "Mbedtls lib link" "")
option(IOTC_INK_CJSON "CJSON lib link" "")

if (TARGET_PLATFORM STREQUAL "linux")
    set(OS_LINUX TRUE)
elseif (TARGET_PLATFORM STREQUAL "android")
    set(OS_ANDROID TRUE)
endif()

add_subdirectory(adapter)
add_subdirectory(core)

set(IOTC_LNK_LIST
    ${IOTC_LNK_LIST}
    ${IOTC_INK_MBEDTLS}
    ${IOTC_INK_CJSON}
)

set(IOTC_INC_LIST
    ${IOTC_INC_LIST}
    ${IOTC_INC_MBEDTLS}
    ${IOTC_INC_CJSON}
    ${CMAKE_CURRENT_SOURCE_DIR}/interfaces/kits/common
    ${CMAKE_CURRENT_SOURCE_DIR}/interfaces/kits/oh_connect
)
if (UT_TEST OR FUZZ_TEST)
    add_subdirectory(tests)
endif()

if (ENABLE_WIFI)
    add_subdirectory(sdk)

    set(IOTC_TARGET OHOS_IOT_DEVICE)
    set(IOTC_LIB_NAME "iotc_device")
    set(IOTC_INC_LIST
        ${IOTC_INC_LIST}
        ${CMAKE_CURRENT_SOURCE_DIR}/core/wifi/interfaces/include
        ${CMAKE_CURRENT_SOURCE_DIR}/core/wifi/infrastructure/utils/include
        ${CMAKE_CURRENT_SOURCE_DIR}/adapter/wifi/linux
    )

    add_library(${IOTC_TARGET} SHARED ${IOTC_SRC_LIST})

    install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/core/wifi/infrastructure/utils/include
            DESTINATION ${CMAKE_INSTALL_PREFIX}/include/utils)

    install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/core/wifi/interfaces/include
            DESTINATION ${CMAKE_INSTALL_PREFIX}/include/interfaces)
elseif (ENABLE_SLE)
    add_subdirectory(sdk)

    set(IOTC_TARGET OHOS_IOT_BRIDGE)
    set(IOTC_LIB_NAME "iotc_bridge")
    set(IOTC_INC_LIST
        ${IOTC_INC_LIST}
        ${CMAKE_CURRENT_SOURCE_DIR}/core/sle/application/profile/include
        ${CMAKE_CURRENT_SOURCE_DIR}/core/sle/application/profile/ssapc
        ${CMAKE_CURRENT_SOURCE_DIR}/core/sle/infrastructure/linklayer
        ${CMAKE_CURRENT_SOURCE_DIR}/core/sle/infrastructure/linklayer/include
        ${CMAKE_CURRENT_SOURCE_DIR}/core/sle/infrastructure/stack/ssap
        ${CMAKE_CURRENT_SOURCE_DIR}/core/sle/infrastructure/stack/ssap/include
        ${CMAKE_CURRENT_SOURCE_DIR}/core/sle/infrastructure/utils/include
        ${CMAKE_CURRENT_SOURCE_DIR}/core/sle/interfaces/include
    )

    add_library(${IOTC_TARGET} SHARED ${IOTC_SRC_LIST})

    install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/core/sle/application/profile/include
            DESTINATION ${CMAKE_INSTALL_PREFIX}/include/profile)

    install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/core/sle/infrastructure/linklayer
            DESTINATION ${CMAKE_INSTALL_PREFIX}/include/linklayer)

    install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/core/sle/infrastructure/stack/ssap
            DESTINATION ${CMAKE_INSTALL_PREFIX}/include/ssap)
else()
    set(IOTC_TARGET OHOS_IOT_UTILS)
    set(IOTC_LIB_NAME "iotc_utils")

    add_library(${IOTC_TARGET} SHARED ${IOTC_SRC_LIST})

    install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/adapter/include/
            DESTINATION ${CMAKE_INSTALL_PREFIX}/include/adapter)

    install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/interfaces/kits/
            DESTINATION ${CMAKE_INSTALL_PREFIX}/include)
endif()

target_include_directories(${IOTC_TARGET} PRIVATE ${IOTC_INC_LIST})
target_link_directories(${IOTC_TARGET} PRIVATE ${IOTC_LNK_LIST})

target_compile_options(${IOTC_TARGET} PRIVATE ${CC_ALL_OPTIONS})
set_target_properties(${IOTC_TARGET} PROPERTIES OUTPUT_NAME ${IOTC_LIB_NAME})

install(TARGETS ${IOTC_TARGET}
    LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)

add_custom_command(TARGET ${IOTC_TARGET}
    COMMENT "${IOTC_LIB_NAME} install"
    POST_BUILD
    COMMAND $(MAKE) install
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
    VERBATIM)